<template>


  <div class="app-container">



    <el-card shadow="always" style="margin-top: 20px;">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="160px">
    <el-row :gutter="20">
      <el-col :span="22">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="所属方向" prop="deptId">
            <el-select v-model="queryParams.deptId" placeholder="请选择所属研究方向" clearable>
                <el-option
                  v-for="dept in deptList"
                  :key="dept.deptId"
                  :label="dept.deptName"
                  :value="dept.deptId"
                />

              </el-select>
          </el-form-item>
          <el-form-item label="项目名称" prop="projectName">
            <el-input
              v-model="queryParams.projectName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="研究领域" prop="researchField">
            <el-input
              v-model="queryParams.researchField"
              placeholder="请输入研究领域"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="负责人" prop="projectLeader">
            <el-input
              v-model="queryParams.projectLeader"
              placeholder="请输入负责人"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>



        </el-col>
        <el-col :span="24">
          <el-form-item label="项目状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择项目状态" clearable>
            <el-option
            v-for="dict in dict.type.sci_pjt_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            />
          </el-select>
          </el-form-item>
          <el-form-item label="开始时间" prop="startDate">
          <el-date-picker clearable
            v-model="queryParams.startDate"
            type="date"
             style="width: 208px"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endDate">
          <el-date-picker clearable
            v-model="queryParams.endDate"
            type="date"
            style="width: 208px"
            value-format="yyyy-MM-dd"
            placeholder="请选择项目结束时间">
          </el-date-picker>
          </el-form-item>

		 <el-form-item label="项目进度" prop="progress">
			<el-input
			  v-model="queryParams.progress"
			  placeholder="请输入项目进度"
			  clearable
			  @keyup.enter.native="handleQuery"
			/>
		  </el-form-item>

        </el-col>
      </el-row>
      </el-col>

      <!-- 第二列 -->
      <el-col :span="2">
        <el-row>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
      </el-col>
      </el-row>
    </el-form>
    </el-card>
    <el-card shadow="always" style="margin-top: 20px;">
    <el-row :gutter="10" class="mb8">


      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:projectManagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名" align="center" prop="projectName" />
      <el-table-column label="研究领域" align="center" prop="researchField" width="150"/>
      <el-table-column label="所属方向" align="center" width="260" prop="deptId">
        <template slot-scope="scope">
          {{ getDeptLabel(scope.row.deptId) }}
        </template>
      </el-table-column>
      <el-table-column label="项目负责人" align="center" prop="projectLeader" width="100"/>
      <el-table-column label="立项时间" align="center" prop="approvalTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startDate" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目结束时间" align="center" prop="endDate" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="项目经费(万元)"
        align="center"
        prop="funding"
      >
        <template slot-scope="scope">
          ¥{{ scope.row.funding ? scope.row.funding.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',') : '0.00' }}
        </template>
      </el-table-column>
      <el-table-column label="项目进度" align="center" prop="progress" >
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="28"
          :percentage="scope.row.progress" :color="(percentage) => customColorMethod(percentage)"></el-progress>
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sci_pjt_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作"  width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button
               type="text"
              style="color: #409EFF;"
              @click="handleView(scope.row)"
              v-hasPermi="['system:projectManagement:query']"
            >查看</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </el-card>



  </div>
</template>

<script>
import { listProjectInfo } from "@/api/sci/projectInfo";
import { listDept } from '@/api/system/dept';




export default {
  name: "ProjectManagement",
  dicts: ['sci_pjt_status','sci_member_role'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],

      // 子表选中数据
      checkedSciProjectTopic: [],

      checkedSciProjectMember: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目征集发布表格数据
      projectInfoList: [],

      // 科研项目课题表格数据
      sciProjectTopicList: [],

      sciProjectMemberList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

	    daterangeApprovalTime: [],


	    deptList: [],





      // 查询参数
      queryParams: {
         pageNum: 1,
        pageSize: 10,
        projectName: null,
        researchField: null,
        deptId: null,
        projectLeader: null,
        startDate: null,
        approvalTime: null,
        progress: null,
        status: null,
        endDate: null
      },

      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
	  this.getDeptList();
  },


  methods: {
    /** 查询项目征集发布列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeApprovalTime && '' != this.daterangeApprovalTime) {
        this.queryParams.params["beginApprovalTime"] = this.daterangeApprovalTime[0];
        this.queryParams.params["endApprovalTime"] = this.daterangeApprovalTime[1];
      }
      listProjectInfo(this.queryParams).then(response => {
        this.projectInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },







    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    handleView(row) {
      const ids = row.id || this.ids
      this.$router.push({
        name: 'ProjectInfo',
        query: { id: ids }
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeApprovalTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },





    /** 导出按钮操作 */
    handleExport() {
      this.download('system/projectInfo/export', {
        ...this.queryParams
      }, `projectInfo_${new Date().getTime()}.xlsx`)
    },


    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },



    getDeptList() {
      listDept().then(response => {
        this.deptList = response.data.filter(dept => dept.deptId !== 100);      });
    },
    getDeptLabel(deptId) {
      const dept = this.deptList.find(d => d.deptId === deptId);
      return dept ? dept.deptName : '';
    },


    customColorMethod(percentage) {
      const red = '#ff4949';
      const orange = '#e6a23c';
      const green = '#67c23a';

      if (percentage < 50) {
        // 红 → 橙
        const ratio = percentage / 50;
        return this.interpolateColor(red, orange, ratio);
      } else {
        // 橙 → 绿
        const ratio = (percentage - 50) / 50;
        return this.interpolateColor(orange, green, ratio);
      }
    },

    interpolateColor(startColor, endColor, ratio) {
      // 将 hex 颜色转换为 RGB
      function hexToRgb(hex) {
        const bigint = parseInt(hex.slice(1), 16);
        const r = (bigint >> 16) & 255;
        const g = (bigint >> 8) & 255;
        const b = bigint & 255;
        return [r, g, b];
      }

      function rgbToHex(rgb) {
        return "#" + rgb.map(c => Math.round(c).toString(16).padStart(2, '0')).join('');
      }

      const start = hexToRgb(startColor);
      const end = hexToRgb(endColor);

      const r = Math.round(start[0] + (end[0] - start[0]) * ratio);
      const g = Math.round(start[1] + (end[1] - start[1]) * ratio);
      const b = Math.round(start[2] + (end[2] - start[2]) * ratio);

      return rgbToHex([r, g, b]);
    }



  }
};
</script>
<style lang="scss" scoped>
.black-bold-header .el-card__header span {
  font-weight: bold;
  font-family: "SimHei", sans-serif;
}
</style>
